#include "luc\watershed.h"
#include "ip.h"

using namespace pip;
using namespace std;

int main(int argc, char **argv)
{

	Stopwatch sw;
		sw.start();
		if (argc < 2){
			cout << "\nUsage: PROGRAMNAME -i INPUT-FILENAME \n { -o OUTPUT-FILENAME }\n";
			cout << "If no image is passed - the lungs.pip Image is assumed. (from region growing assignment - 30MB) \n";
		}
        // Store commandline args in stl container
        vector<string> args(argv, argv+argc);

        // Variables to be read from the commandline
        string program, markers(""), inputfile(""), outputfile = "result.pip";
        
        // Read the arguments.
        getProgramName(args, program);
        getScalarArg(args, "-i", inputfile);
        getScalarArg(args, "-o", markers);

		Image<unsigned char> in,marks;
		Image<unsigned char> output;

			if (!importFile(marks, markers)) {
				cout << "fail\n";;
			}else{cout << "loaded\n";}
		
			if (!importFile(in, inputfile)) {
				cout << "fail\n";;
			}else{cout << "loaded\n";}

		double s_final  = 2;
		int s = 1;
		int count(0),xd(in.dim(0));


		//Image<float> result(Dimension(xd*4,in.dim(1)*s,int(s_final)));
		Image<unsigned char> blur,tmp,tmp2,se,se2,mark(Dimension(xd,in.dim(1)));
		mark.fill(unsigned char(0));
		Image<float> inputf,outputf,ero,dil,mg,grad(Dimension(xd,in.dim(1)));
		
		discStructureElement(se,3);
		mg.resize(in.dimension());
		mg.fill(0);
		grad.resize(in.dimension());
		grad.fill(0);

		for(int x=0;x<xd;++x){
				for(int y=0;y<in.dim(1);++y){
					if(in(x,y)!=marks(x,y)){
					   mark(x,y)=unsigned char(255);
					

					   cout << "found marker" << endl;
						}
				}
			}
		convertUnsignedCharToFloat(in, inputf);




		//cannyEdgeDetector(in,mg, 1);
		for(s=2 ; s <= s_final; s++){	
			discStructureElement(se,s);
			discStructureElement(se2,(s-1));
			//gauss(inputf, outputf, float(s*2), 0, 0, 5);
			//convertFloatToUnsignedChar(outputf,blur);
			grayDilate(inputf, dil,se);
			grayErode(inputf, ero,se);
			for(int x=0;x<xd;++x){
				for(int y=0;y<in.dim(1);++y){
					//cout << dil(x,y) << "/" << ero(x,y)<< " . ";
					grad(x,y)=dil(x,y)-ero(x,y);
				}
			}
			grayErode(grad, ero,se2);
			for(int x=0;x<xd;++x){
				for(int y=0;y<in.dim(1);++y){
					mg(x,y)=mg(x,y)+ero(x,y);
				}
			}

			//convertFloatToUnsignedChar(mg,tmp);
			//cannyEdgeDetector(tmp, outputf,1);
			//convertFloatToUnsignedChar(outputf,tmp);
			//convertFloatToUnsignedChar(inputf,in);
			//watershed(tmp, tmp2);
			/*for(int x=0;x<xd;++x){
				for(int y=0;y<in.dim(1);++y){
					result(x,y+((s-1)*in.dim(1)))             =outputf(x,y);
					result(x+xd,y+((s-1)*in.dim(1)))          =tmp(x,y);
					result(x+(xd*2),y+((s-1)*in.dim(1)))      =tmp2(x,y);
					if(count>0)
						result(x+(xd*3),y+((s-1)*in.dim(1)))   =result(x+(xd*3),y+(s-2));
					if(tmp2(x,y)==unsigned char(0))
						result(x+(xd*3),y+((s-1)*in.dim(1)))  +=float(250/s_final);			
				}
			}*/

			count++;
		}
		for(int x=0;x<xd;++x){
				for(int y=0;y<in.dim(1);++y){
					mg(x,y)=mg(x,y)/s;
				}
		}


		convertFloatToUnsignedChar(mg,tmp);

for(int x=0;x<xd;++x){
				for(int y=0;y<in.dim(1);++y){
					if(in(x,y)!=marks(x,y)){
					  tmp(x,y)=0;
						}
				}
			}

		watershed(tmp, tmp2,mark);
		if (!exportFile(tmp, "outcomeb4.pip")) {
			 error("", "can't save file");
		}
if (!exportFile(mark, "markers_.pip")) {
			 error("", "can't save file");
		}

		if (!exportFile(tmp2, "outcome.pip")) {
			 error("", "can't save file");
		}
		cout << "The result can be fount in 'outcome.pip', a 3d Image. \nThe z-axis is the scale space.\nFirst Image is the scaled one.\nSecond is the 'GRAD'\n 3rd is the watershed segmented Image. \n 4th Image is the density over all water-shed boarders over all segmentations 'so far'.\n BEWARE the Images for z==0 are still empty (sorry)...";

		cout << sw.stop() <<endl;
		system("pause");
        return 0;

			}